package cn.dxc.demo.config;


import cn.dxc.demo.entity.User;
import cn.dxc.demo.service.IUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private IUserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    /***
     * 用户登录校验 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String username = token.getUsername();
        User userQuery = new User();
        userQuery.setUsername(username);
        System.out.println(userQuery);
        User user = userService.findUserByParam(userQuery);

        if (user == null){
            return null;
        }

        return new SimpleAuthenticationInfo(user,user.getPassword(),getName());
    }
}
